#ifndef esda_SQLOperatorStream_h
#define esda_SQLOperatorStream_h

/*
 * Copyright (C) Olivier Le Floch
 *
 * See the file LICENSE for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "common.h"
#include "Timestamp.h"
#include "Streams.h"

namespace esda
{
	template<typename Value>
	class SQLOperatorStream : public SQLiteStream<Value> {
	private:
		
	public:
		SQLOperatorStream(
			const string& databaseName,
			const string& viewName,
			const string& idClause,
			const string& startTimeClause,
			const string& endTimeClause,
			const string& valueClause,
			const string& fromClause,
			const string& whereClause);
		
		// TODO : repair this !
		SQLOperatorStream(const SQLOperatorStream& SourceStream) {};
		
		// SQLOperatorStream &operator= (const SQLOperatorStream& SourceStream) {
		// 	database           = SourceStream.database;
		// 	getQuery           = SourceStream.getQuery;
		// 	fromAndWhereClause = SourceStream.fromAndWhereClause;
		// 	connected          = SourceStream.connected;
		// 	
		// 	return *this;
		// };
	};
	
	template<typename Value>
	SQLOperatorStream<Value>::SQLOperatorStream(
		const string& databaseName,
		const string& viewName,
		const string& idClause,
		const string& startTimeClause,
		const string& endTimeClause,
		const string& valueClause,
		const string& fromClause,
		const string& whereClause)
		: SQLiteStream<Value>(databaseName, viewName, false)
	{
		// Création de la vue
		
		SQLiteStream<Value>::runCreateQuery(
		        "CREATE VIEW IF NOT EXISTS `" + viewName + "` AS " +
		        "SELECT " + idClause        + " AS Id, " +
		                    startTimeClause + " AS StartTime, " +
		                    endTimeClause   + " AS EndTime, " + 
		                    valueClause     + " AS Value " +
		        "FROM "   + fromClause +
		        " WHERE "  + whereClause);
	}
}

#endif
